package com.hy.mydesktop.shared.rpc.util;

import com.extjs.gxt.ui.client.data.BaseModel;

public class BaseModelUtil {

	public static void encapsulateComponentData(BaseModel baseModel,String key,Object value) {
		//String key=null;
		int firstIndex = key.indexOf(".");
		int secondIndex = -1;
		int thirdIndex = -1;
		System.out.println(firstIndex);
		if (firstIndex > -1) {
			secondIndex = key.indexOf(".", firstIndex+1);	
		}
		if (secondIndex>-1) {
			thirdIndex = key.indexOf(".", secondIndex+1);	
		}
		String componentName = null;
		String subComponentName = null;
		
		//搭建嵌套的结构
		if (firstIndex>-1) {
			componentName = key.substring(0, firstIndex);
			if (baseModel.get(componentName)==null) {
				baseModel.set(componentName, new BaseModel());

			}else if (baseModel.get(componentName) instanceof BaseModel) {
				//
			}else {
				baseModel.set(componentName, new BaseModel());
			}
		
			if (secondIndex>-1) {
				subComponentName = key.substring(firstIndex+1, secondIndex);
				if (baseModel.get(key.substring(0, secondIndex))==null) {
					baseModel.set(key.substring(0, secondIndex), new BaseModel());

				}else if (baseModel.get(key.substring(0, secondIndex)) instanceof BaseModel) {
					//
				}else {
					baseModel.set(key.substring(0, secondIndex), new BaseModel());
				}
				
				
			} 
		}
		baseModel.set(key,value);

	}
	
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：取得当前字符串中，最后一个英文句号，之前的部分</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-12-14；时间：下午下午02:31:01</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param componentId
	 * @return
	 */
	public static String getPrefix(String componentId) {
		int lastIndex = componentId.lastIndexOf(".");
		return componentId.substring(0, lastIndex);
	}
	
	public static String getFirstPrefix(String componentId) {
		int lastIndex = componentId.indexOf(".");
		if (lastIndex==-1) {
			return componentId;
		} else {
			return componentId.substring(0, lastIndex);

		}
	}
	

	public static int getFirstPrefixIndex(String componentId) {
		int firstIndex = componentId.indexOf(".");
		if (firstIndex==-1) {
			return -1;
		} else {
			return firstIndex;

		}
	}
	
}
